package com.epam.edu.command;

import com.epam.edu.constant.Language;
import com.epam.edu.dao.DAOAdmission;
import com.epam.edu.dao.DAOSpecialty;
import com.epam.edu.dao.DAOTeachingForm;
import com.epam.edu.dao.DAOTeachingPlan;
import com.epam.edu.db.ConnectionPool;
import com.epam.edu.entity.Admission;
import com.epam.edu.entity.Specialty;
import com.epam.edu.entity.TeachingForm;
import com.epam.edu.entity.TeachingPlan;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class CatalogAdmissionsCommand implements Command {
    @Override
    public String perform(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, NoSuchAlgorithmException {
        ConnectionPool pool = null;
        Connection connection = null;
        try {
            ConnectionPool.init();
            pool = ConnectionPool.getInstance();
            connection = pool.takeConnection();
            List<TeachingForm> teachingForms = DAOTeachingForm.selectAll(connection);
            request.setAttribute("teachingForms", teachingForms);
            List<Specialty> specialties = DAOSpecialty.selectAll(connection);
            request.setAttribute("specialties", specialties);
            List<TeachingPlan> teachingPlans = DAOTeachingPlan.selectAll(connection);
            request.setAttribute("teachingPlans", teachingPlans);
            List<Admission> admissions = DAOAdmission.selectAll(connection);
            request.setAttribute("admissions", admissions);
            List<String> languages = new LinkedList<String>();
            for (Language lang : Language.values()) {
                languages.add(lang.toString());
            }
            request.setAttribute("languages", languages);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "/WEB-INF/jsp/admin/catalogAdmissions.jsp";
    }
}
